﻿using DataBaseBackupTools.FmChild;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using ModelLibrary;

namespace DataBaseBackupTools
{
    public partial class FmInstance : Form
    {
        public FmInstance()
        {
            InitializeComponent();
            this.TopLevel = false;
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(dataGridView1_RowPrePaint);
            RefreshList();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int dgvrIndex = e.RowIndex;
            FmInstanceChild FIC = new FmInstanceChild(dgvrIndex);
            if (FIC.ShowDialog() == DialogResult.OK)
            {
                RefreshList();
                dataGridView1.CurrentCell = dataGridView1[0, dgvrIndex];
            }
        }
        private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ViewImage();
        }
        private void btnAdd_Click(object sender, EventArgs e)//添加按钮
        {
            FmInstanceChild FIC = new FmInstanceChild();
            if (FIC.ShowDialog() == DialogResult.OK)
            {
                RefreshList();
                this.dataGridView1.CurrentCell = dataGridView1[0, dataGridView1.RowCount - 1];
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)//编辑按钮
        {
            编辑ToolStripMenuItem_Click(null, new EventArgs());
        }

        private void btnDelete_Click(object sender, EventArgs e)//删除按钮
        {
            删除ToolStripMenuItem_Click(null, new EventArgs());
        }

        private void btnStartAll_Click(object sender, EventArgs e)//全部启用按钮
        {
            List<Instance> LiInst = (List<Instance>)dataGridView1.DataSource;
            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                dgvr.Cells["taskStatus"].Value = AppSets.imgTrue;
                LiInst[dgvr.Index].taskStatus = true;
            }
            LiInstances LiInstance = new LiInstances();
            LiInstance.AddRange(LiInst);
            XmlHelper.XmlSerializeToFile(LiInstance, AppSets.InstancePath, AppSets.Encodings);
        }

        private void btnStopAll_Click(object sender, EventArgs e)//全部停用按钮
        {
            List<Instance> LiInst = (List<Instance>)dataGridView1.DataSource;
            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                dgvr.Cells["taskStatus"].Value = AppSets.imgFalse;
                LiInst[dgvr.Index].taskStatus = false;
            }
            LiInstances LiInstance = new LiInstances();
            LiInstance.AddRange(LiInst);
            XmlHelper.XmlSerializeToFile(LiInstance, AppSets.InstancePath, AppSets.Encodings);
        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)//鼠标右键添加
        {
            btnAdd_Click(btnAdd, e);
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)//鼠标右键编辑
        {
            int dgvrIndex = this.dataGridView1.SelectedRows[0].Index;
            FmInstanceChild FIC = new FmInstanceChild(dgvrIndex);
            if (FIC.ShowDialog() == DialogResult.OK)
            {
                RefreshList();
                dataGridView1.CurrentCell = dataGridView1[0, dgvrIndex];
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)//鼠标右键删除
        {
            List<Instance> LiInst = (List<Instance>)dataGridView1.DataSource;
            int dgvrIX = this.dataGridView1.SelectedRows[0].Index;
            this.dataGridView1.DataSource = null;
            LiInst.RemoveAt(dgvrIX);
            LiInstances LiInstance = new LiInstances();
            LiInstance.AddRange(LiInst);
            XmlHelper.XmlSerializeToFile(LiInstance, AppSets.InstancePath, AppSets.Encodings);
            this.dataGridView1.DataSource = LiInst;
            if (dgvrIX < LiInst.Count)
            {
                dataGridView1.CurrentCell = dataGridView1[0, dgvrIX];
            }
            else if (dgvrIX == LiInst.Count && dgvrIX > 0)
            {
                dataGridView1.CurrentCell = dataGridView1[0, dgvrIX - 1];
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)//鼠标右键刷新
        {
            RefreshList();
        }

        private void 启用ToolStripMenuItem_Click(object sender, EventArgs e)//鼠标右键启用
        {
            List<Instance> LiInst = (List<Instance>)dataGridView1.DataSource;
            DataGridViewRow dgvr = this.dataGridView1.SelectedRows[0];
            dgvr.Cells["taskStatus"].Value = AppSets.imgTrue;
            LiInst[dgvr.Index].taskStatus = true;
            LiInstances LiInstance = new LiInstances();
            LiInstance.AddRange(LiInst);
            XmlHelper.XmlSerializeToFile(LiInstance, AppSets.InstancePath, AppSets.Encodings);
        }

        private void 停用ToolStripMenuItem_Click(object sender, EventArgs e)//鼠标右键停用
        {
            List<Instance> LiInst = (List<Instance>)dataGridView1.DataSource;
            DataGridViewRow dgvr = this.dataGridView1.SelectedRows[0];
            dgvr.Cells["taskStatus"].Value = AppSets.imgFalse;
            LiInst[dgvr.Index].taskStatus = false;
            LiInstances LiInstance = new LiInstances();
            LiInstance.AddRange(LiInst);
            XmlHelper.XmlSerializeToFile(LiInstance, AppSets.InstancePath, AppSets.Encodings);
        }

        private void RefreshList()//刷新界面
        {
            if (!System.IO.File.Exists(AppSets.InstancePath))
            {
                return;
            }
            List<Instance> liInst = XmlHelper.XmlDeserializeFromFile<LiInstances>(AppSets.InstancePath, AppSets.Encodings);
            if (liInst.Count == 0 || liInst == null)
            {
                return;
            }
            this.dataGridView1.Columns["sort"].DataPropertyName = "sort";
            this.dataGridView1.Columns["taskName"].DataPropertyName = "taskName";
            this.dataGridView1.Columns["databaseType"].DataPropertyName = "databaseType";
            this.dataGridView1.Columns["linkInfo"].DataPropertyName = "linkInfo";
            this.dataGridView1.Columns["modifyTime"].DataPropertyName = "modifyTime";
            this.dataGridView1.Columns["status"].DataPropertyName = "taskStatus";
            this.dataGridView1.DataSource = liInst;

            //ViewImage();

        }

        private void ViewImage()//刷新图片显示
        {

            foreach (DataGridViewRow dgvr in dataGridView1.Rows)
            {
                if (dgvr.Cells["status"].Value == null)
                {
                    dgvr.Cells["taskStatus"].Value = dgvr.Cells["status"].Value;
                }
                else
                    dgvr.Cells["taskStatus"].Value = (bool)dgvr.Cells["status"].Value ? AppSets.imgTrue : AppSets.imgFalse;
            }

        }

    }
}
